<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>datamanager.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_datasource_datamanager.js.html">datamanager.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview
<a name="line17"></a> * Central class for registering and accessing data sources
<a name="line18"></a> * Also handles processing of data events.
<a name="line19"></a> *
<a name="line20"></a> * There is a shared global instance that most client code should access via
<a name="line21"></a> * goog.ds.DataManager.getInstance(). However you can also create your own
<a name="line22"></a> * DataManager using new
<a name="line23"></a> *
<a name="line24"></a> * Implements DataNode to provide the top element in a data registry
<a name="line25"></a> * Prepends &#39;$&#39; to top level data names in path to denote they are root object
<a name="line26"></a> *
<a name="line27"></a> */
<a name="line28"></a>goog.provide(&#39;goog.ds.DataManager&#39;);
<a name="line29"></a>
<a name="line30"></a>goog.require(&#39;goog.ds.BasicNodeList&#39;);
<a name="line31"></a>goog.require(&#39;goog.ds.DataNode&#39;);
<a name="line32"></a>goog.require(&#39;goog.ds.Expr&#39;);
<a name="line33"></a>goog.require(&#39;goog.string&#39;);
<a name="line34"></a>goog.require(&#39;goog.structs&#39;);
<a name="line35"></a>goog.require(&#39;goog.structs.Map&#39;);
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * Create a DataManger
<a name="line41"></a> * @extends {goog.ds.DataNode}
<a name="line42"></a> * @constructor
<a name="line43"></a> */
<a name="line44"></a>goog.ds.DataManager = function() {
<a name="line45"></a>  this.dataSources_ = new goog.ds.BasicNodeList();
<a name="line46"></a>  this.autoloads_ = new goog.structs.Map();
<a name="line47"></a>  this.listenerMap_ = {};
<a name="line48"></a>  this.listenersByFunction_ = {};
<a name="line49"></a>  this.aliases_ = {};
<a name="line50"></a>  this.eventCount_ = 0;
<a name="line51"></a>  this.indexedListenersByFunction_ = {};
<a name="line52"></a>};
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Global instance
<a name="line57"></a> * @private
<a name="line58"></a> */
<a name="line59"></a>goog.ds.DataManager.instance_ = null;
<a name="line60"></a>goog.inherits(goog.ds.DataManager, goog.ds.DataNode);
<a name="line61"></a>
<a name="line62"></a>
<a name="line63"></a>/**
<a name="line64"></a> * Get the global instance
<a name="line65"></a> * @return {goog.ds.DataManager} The data manager singleton.
<a name="line66"></a> */
<a name="line67"></a>goog.ds.DataManager.getInstance = function() {
<a name="line68"></a>  if (!goog.ds.DataManager.instance_) {
<a name="line69"></a>    goog.ds.DataManager.instance_ = new goog.ds.DataManager();
<a name="line70"></a>  }
<a name="line71"></a>  return goog.ds.DataManager.instance_;
<a name="line72"></a>};
<a name="line73"></a>
<a name="line74"></a>
<a name="line75"></a>/**
<a name="line76"></a> * Clears the global instance (for unit tests to reset state).
<a name="line77"></a> */
<a name="line78"></a>goog.ds.DataManager.clearInstance = function() {
<a name="line79"></a>  goog.ds.DataManager.instance_ = null;
<a name="line80"></a>};
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * Add a data source
<a name="line85"></a> * @param {goog.ds.DataNode} ds The data source.
<a name="line86"></a> * @param {boolean=} opt_autoload Whether to automatically load the data,
<a name="line87"></a> *   defaults to false.
<a name="line88"></a> * @param {string=} opt_name Optional name, can also get name
<a name="line89"></a> *   from the datasource.
<a name="line90"></a> */
<a name="line91"></a>goog.ds.DataManager.prototype.addDataSource = function(ds, opt_autoload,
<a name="line92"></a>    opt_name) {
<a name="line93"></a>  var autoload = !!opt_autoload;
<a name="line94"></a>  var name = opt_name || ds.getDataName();
<a name="line95"></a>  if (!goog.string.startsWith(name, &#39;$&#39;)) {
<a name="line96"></a>    name = &#39;$&#39; + name;
<a name="line97"></a>  }
<a name="line98"></a>  ds.setDataName(name);
<a name="line99"></a>  this.dataSources_.add(ds);
<a name="line100"></a>  this.autoloads_.set(name, autoload);
<a name="line101"></a>};
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Create an alias for a data path, very similar to assigning a variable.
<a name="line106"></a> * For example, you can set $CurrentContact -&gt; $Request/Contacts[5], and all
<a name="line107"></a> * references to $CurrentContact will be procesed on $Request/Contacts[5].
<a name="line108"></a> *
<a name="line109"></a> * Aliases will hide datasources of the same name.
<a name="line110"></a> *
<a name="line111"></a> * @param {string} name Alias name, must be a top level path ($Foo).
<a name="line112"></a> * @param {string} dataPath Data path being aliased.
<a name="line113"></a> */
<a name="line114"></a>goog.ds.DataManager.prototype.aliasDataSource = function(name, dataPath) {
<a name="line115"></a>  if (!this.aliasListener_) {
<a name="line116"></a>    this.aliasListener_ = goog.bind(this.listenForAlias_, this);
<a name="line117"></a>  }
<a name="line118"></a>  if (this.aliases_[name]) {
<a name="line119"></a>    var oldPath = this.aliases_[name].getSource();
<a name="line120"></a>    this.removeListeners(this.aliasListener_, oldPath + &#39;/...&#39;, name);
<a name="line121"></a>  }
<a name="line122"></a>  this.aliases_[name] = goog.ds.Expr.create(dataPath);
<a name="line123"></a>  this.addListener(this.aliasListener_, dataPath + &#39;/...&#39;, name);
<a name="line124"></a>  this.fireDataChange(name);
<a name="line125"></a>};
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * Listener function for matches of paths that have been aliased.
<a name="line130"></a> * Fires a data change on the alias as well.
<a name="line131"></a> *
<a name="line132"></a> * @param {string} dataPath Path of data event fired.
<a name="line133"></a> * @param {string} name Name of the alias.
<a name="line134"></a> * @private
<a name="line135"></a> */
<a name="line136"></a>goog.ds.DataManager.prototype.listenForAlias_ = function(dataPath, name) {
<a name="line137"></a>  var aliasedExpr = this.aliases_[name];
<a name="line138"></a>
<a name="line139"></a>  if (aliasedExpr) {
<a name="line140"></a>    // If it&#39;s a subpath, appends the subpath to the alias name
<a name="line141"></a>    // otherwise just fires on the top level alias
<a name="line142"></a>    var aliasedPath = aliasedExpr.getSource();
<a name="line143"></a>    if (dataPath.indexOf(aliasedPath) == 0) {
<a name="line144"></a>      this.fireDataChange(name + dataPath.substring(aliasedPath.length));
<a name="line145"></a>    } else {
<a name="line146"></a>      this.fireDataChange(name);
<a name="line147"></a>    }
<a name="line148"></a>  }
<a name="line149"></a>};
<a name="line150"></a>
<a name="line151"></a>
<a name="line152"></a>/**
<a name="line153"></a> * Gets a named child node of the current node.
<a name="line154"></a> *
<a name="line155"></a> * @param {string} name The node name.
<a name="line156"></a> * @return {goog.ds.DataNode} The child node,
<a name="line157"></a> *   or null if no node of this name exists.
<a name="line158"></a> */
<a name="line159"></a>goog.ds.DataManager.prototype.getDataSource = function(name) {
<a name="line160"></a>  if (this.aliases_[name]) {
<a name="line161"></a>    return this.aliases_[name].getNode();
<a name="line162"></a>  } else {
<a name="line163"></a>    return this.dataSources_.get(name);
<a name="line164"></a>  }
<a name="line165"></a>};
<a name="line166"></a>
<a name="line167"></a>
<a name="line168"></a>/**
<a name="line169"></a> * Get the value of the node
<a name="line170"></a> * @return {Object} The value of the node, or null if no value.
<a name="line171"></a> * @override
<a name="line172"></a> */
<a name="line173"></a>goog.ds.DataManager.prototype.get = function() {
<a name="line174"></a>  return this.dataSources_;
<a name="line175"></a>};
<a name="line176"></a>
<a name="line177"></a>
<a name="line178"></a>/** @override */
<a name="line179"></a>goog.ds.DataManager.prototype.set = function(value) {
<a name="line180"></a>  throw Error(&#39;Can\&#39;t set on DataManager&#39;);
<a name="line181"></a>};
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/** @override */
<a name="line185"></a>goog.ds.DataManager.prototype.getChildNodes = function(opt_selector) {
<a name="line186"></a>  if (opt_selector) {
<a name="line187"></a>    return new goog.ds.BasicNodeList(
<a name="line188"></a>        [this.getChildNode(/** @type {string} */(opt_selector))]);
<a name="line189"></a>  } else {
<a name="line190"></a>    return this.dataSources_;
<a name="line191"></a>  }
<a name="line192"></a>};
<a name="line193"></a>
<a name="line194"></a>
<a name="line195"></a>/**
<a name="line196"></a> * Gets a named child node of the current node
<a name="line197"></a> * @param {string} name The node name.
<a name="line198"></a> * @return {goog.ds.DataNode} The child node,
<a name="line199"></a> *     or null if no node of this name exists.
<a name="line200"></a> * @override
<a name="line201"></a> */
<a name="line202"></a>goog.ds.DataManager.prototype.getChildNode = function(name) {
<a name="line203"></a>  return this.getDataSource(name);
<a name="line204"></a>};
<a name="line205"></a>
<a name="line206"></a>
<a name="line207"></a>/** @override */
<a name="line208"></a>goog.ds.DataManager.prototype.getChildNodeValue = function(name) {
<a name="line209"></a>  var ds = this.getDataSource(name);
<a name="line210"></a>  return ds ? ds.get() : null;
<a name="line211"></a>};
<a name="line212"></a>
<a name="line213"></a>
<a name="line214"></a>/**
<a name="line215"></a> * Get the name of the node relative to the parent node
<a name="line216"></a> * @return {string} The name of the node.
<a name="line217"></a> * @override
<a name="line218"></a> */
<a name="line219"></a>goog.ds.DataManager.prototype.getDataName = function() {
<a name="line220"></a>  return &#39;&#39;;
<a name="line221"></a>};
<a name="line222"></a>
<a name="line223"></a>
<a name="line224"></a>/**
<a name="line225"></a> * Gets the a qualified data path to this node
<a name="line226"></a> * @return {string} The data path.
<a name="line227"></a> * @override
<a name="line228"></a> */
<a name="line229"></a>goog.ds.DataManager.prototype.getDataPath = function() {
<a name="line230"></a>  return &#39;&#39;;
<a name="line231"></a>};
<a name="line232"></a>
<a name="line233"></a>
<a name="line234"></a>/**
<a name="line235"></a> * Load or reload the backing data for this node
<a name="line236"></a> * only loads datasources flagged with autoload
<a name="line237"></a> * @override
<a name="line238"></a> */
<a name="line239"></a>goog.ds.DataManager.prototype.load = function() {
<a name="line240"></a>  var len = this.dataSources_.getCount();
<a name="line241"></a>  for (var i = 0; i &lt; len; i++) {
<a name="line242"></a>    var ds = this.dataSources_.getByIndex(i);
<a name="line243"></a>    var autoload = this.autoloads_.get(ds.getDataName());
<a name="line244"></a>    if (autoload) {
<a name="line245"></a>      ds.load();
<a name="line246"></a>    }
<a name="line247"></a>  }
<a name="line248"></a>};
<a name="line249"></a>
<a name="line250"></a>
<a name="line251"></a>/**
<a name="line252"></a> * Gets the state of the backing data for this node
<a name="line253"></a> * @return {goog.ds.LoadState} The state.
<a name="line254"></a> * @override
<a name="line255"></a> */
<a name="line256"></a>goog.ds.DataManager.prototype.getLoadState = goog.abstractMethod;
<a name="line257"></a>
<a name="line258"></a>
<a name="line259"></a>/**
<a name="line260"></a> * Whether the value of this node is a homogeneous list of data
<a name="line261"></a> * @return {boolean} True if a list.
<a name="line262"></a> * @override
<a name="line263"></a> */
<a name="line264"></a>goog.ds.DataManager.prototype.isList = function() {
<a name="line265"></a>  return false;
<a name="line266"></a>};
<a name="line267"></a>
<a name="line268"></a>
<a name="line269"></a>/**
<a name="line270"></a> * Get the total count of events fired (mostly for debugging)
<a name="line271"></a> * @return {number} Count of events.
<a name="line272"></a> */
<a name="line273"></a>goog.ds.DataManager.prototype.getEventCount = function() {
<a name="line274"></a>  return this.eventCount_;
<a name="line275"></a>};
<a name="line276"></a>
<a name="line277"></a>
<a name="line278"></a>/**
<a name="line279"></a> * Adds a listener
<a name="line280"></a> * Listeners should fire when any data with path that has dataPath as substring
<a name="line281"></a> * is changed.
<a name="line282"></a> * TODO(user) Look into better listener handling
<a name="line283"></a> *
<a name="line284"></a> * @param {Function} fn Callback function, signature function(dataPath, id).
<a name="line285"></a> * @param {string} dataPath Fully qualified data path.
<a name="line286"></a> * @param {string=} opt_id A value passed back to the listener when the dataPath
<a name="line287"></a> *   is matched.
<a name="line288"></a> */
<a name="line289"></a>goog.ds.DataManager.prototype.addListener = function(fn, dataPath, opt_id) {
<a name="line290"></a>  // maxAncestor sets how distant an ancestor you can be of the fired event
<a name="line291"></a>  // and still fire (you always fire if you are a descendant).
<a name="line292"></a>  // 0 means you don&#39;t fire if you are an ancestor
<a name="line293"></a>  // 1 means you only fire if you are parent
<a name="line294"></a>  // 1000 means you will fire if you are ancestor (effectively infinite)
<a name="line295"></a>  var maxAncestors = 0;
<a name="line296"></a>  if (goog.string.endsWith(dataPath, &#39;/...&#39;)) {
<a name="line297"></a>    maxAncestors = 1000;
<a name="line298"></a>    dataPath = dataPath.substring(0, dataPath.length - 4);
<a name="line299"></a>  } else if (goog.string.endsWith(dataPath, &#39;/*&#39;)) {
<a name="line300"></a>    maxAncestors = 1;
<a name="line301"></a>    dataPath = dataPath.substring(0, dataPath.length - 2);
<a name="line302"></a>  }
<a name="line303"></a>
<a name="line304"></a>  opt_id = opt_id || &#39;&#39;;
<a name="line305"></a>  var key = dataPath + &#39;:&#39; + opt_id + &#39;:&#39; + goog.getUid(fn);
<a name="line306"></a>  var listener = {dataPath: dataPath, id: opt_id, fn: fn};
<a name="line307"></a>  var expr = goog.ds.Expr.create(dataPath);
<a name="line308"></a>
<a name="line309"></a>  var fnUid = goog.getUid(fn);
<a name="line310"></a>  if (!this.listenersByFunction_[fnUid]) {
<a name="line311"></a>    this.listenersByFunction_[fnUid] = {};
<a name="line312"></a>  }
<a name="line313"></a>  this.listenersByFunction_[fnUid][key] = {listener: listener, items: []};
<a name="line314"></a>
<a name="line315"></a>  while (expr) {
<a name="line316"></a>    var listenerSpec = {listener: listener, maxAncestors: maxAncestors};
<a name="line317"></a>    var matchingListeners = this.listenerMap_[expr.getSource()];
<a name="line318"></a>    if (matchingListeners == null) {
<a name="line319"></a>      matchingListeners = {};
<a name="line320"></a>      this.listenerMap_[expr.getSource()] = matchingListeners;
<a name="line321"></a>    }
<a name="line322"></a>    matchingListeners[key] = listenerSpec;
<a name="line323"></a>    maxAncestors = 0;
<a name="line324"></a>    expr = expr.getParent();
<a name="line325"></a>    this.listenersByFunction_[fnUid][key].items.push(
<a name="line326"></a>        {key: key, obj: matchingListeners});
<a name="line327"></a>  }
<a name="line328"></a>};
<a name="line329"></a>
<a name="line330"></a>
<a name="line331"></a>/**
<a name="line332"></a> * Adds an indexed listener.
<a name="line333"></a> *
<a name="line334"></a> * Indexed listeners allow for &#39;*&#39; in data paths. If a * exists, will match
<a name="line335"></a> * all values and return the matched values in an array to the callback.
<a name="line336"></a> *
<a name="line337"></a> * Currently uses a promiscuous match algorithm: Matches everything before the
<a name="line338"></a> * first &#39;*&#39;, and then does a regex match for all of the returned events.
<a name="line339"></a> * Although this isn&#39;t optimized, it is still an improvement as you can collapse
<a name="line340"></a> * 100&#39;s of listeners into a single regex match
<a name="line341"></a> *
<a name="line342"></a> * @param {Function} fn Callback function, signature (dataPath, id, indexes).
<a name="line343"></a> * @param {string} dataPath Fully qualified data path.
<a name="line344"></a> * @param {string=} opt_id A value passed back to the listener when the dataPath
<a name="line345"></a> *   is matched.
<a name="line346"></a> */
<a name="line347"></a>goog.ds.DataManager.prototype.addIndexedListener = function(fn, dataPath,
<a name="line348"></a>    opt_id) {
<a name="line349"></a>  var firstStarPos = dataPath.indexOf(&#39;*&#39;);
<a name="line350"></a>  // Just need a regular listener
<a name="line351"></a>  if (firstStarPos == -1) {
<a name="line352"></a>    this.addListener(fn, dataPath, opt_id);
<a name="line353"></a>    return;
<a name="line354"></a>  }
<a name="line355"></a>
<a name="line356"></a>  var listenPath = dataPath.substring(0, firstStarPos) + &#39;...&#39;;
<a name="line357"></a>
<a name="line358"></a>  // Create regex that matches * to any non &#39;\&#39; character
<a name="line359"></a>  var ext = &#39;$&#39;;
<a name="line360"></a>  if (goog.string.endsWith(dataPath, &#39;/...&#39;)) {
<a name="line361"></a>    dataPath = dataPath.substring(0, dataPath.length - 4);
<a name="line362"></a>    ext = &#39;&#39;;
<a name="line363"></a>  }
<a name="line364"></a>  var regExpPath = goog.string.regExpEscape(dataPath);
<a name="line365"></a>  var matchRegExp = regExpPath.replace(/\\\*/g, &#39;([^\\\/]+)&#39;) + ext;
<a name="line366"></a>
<a name="line367"></a>  // Matcher function applies the regex and calls back the original function
<a name="line368"></a>  // if the regex matches, passing in an array of the matched values
<a name="line369"></a>  var matchRegExpRe = new RegExp(matchRegExp);
<a name="line370"></a>  var matcher = function(path, id) {
<a name="line371"></a>    var match = matchRegExpRe.exec(path);
<a name="line372"></a>    if (match) {
<a name="line373"></a>      match.shift();
<a name="line374"></a>      fn(path, opt_id, match);
<a name="line375"></a>    }
<a name="line376"></a>  }
<a name="line377"></a>  this.addListener(matcher, listenPath, opt_id);
<a name="line378"></a>
<a name="line379"></a>  // Add the indexed listener to the map so that we can remove it later.
<a name="line380"></a>  var fnUid = goog.getUid(fn);
<a name="line381"></a>  if (!this.indexedListenersByFunction_[fnUid]) {
<a name="line382"></a>    this.indexedListenersByFunction_[fnUid] = {};
<a name="line383"></a>  }
<a name="line384"></a>  var key = dataPath + &#39;:&#39; + opt_id;
<a name="line385"></a>  this.indexedListenersByFunction_[fnUid][key] = {
<a name="line386"></a>    listener: {dataPath: listenPath, fn: matcher, id: opt_id}
<a name="line387"></a>  };
<a name="line388"></a>};
<a name="line389"></a>
<a name="line390"></a>
<a name="line391"></a>/**
<a name="line392"></a> * Removes indexed listeners with a given callback function, and optional
<a name="line393"></a> * matching datapath and matching id.
<a name="line394"></a> *
<a name="line395"></a> * @param {Function} fn Callback function, signature function(dataPath, id).
<a name="line396"></a> * @param {string=} opt_dataPath Fully qualified data path.
<a name="line397"></a> * @param {string=} opt_id A value passed back to the listener when the dataPath
<a name="line398"></a> *   is matched.
<a name="line399"></a> */
<a name="line400"></a>goog.ds.DataManager.prototype.removeIndexedListeners = function(
<a name="line401"></a>    fn, opt_dataPath, opt_id) {
<a name="line402"></a>  this.removeListenersByFunction_(
<a name="line403"></a>      this.indexedListenersByFunction_, true, fn, opt_dataPath, opt_id);
<a name="line404"></a>};
<a name="line405"></a>
<a name="line406"></a>
<a name="line407"></a>/**
<a name="line408"></a> * Removes listeners with a given callback function, and optional
<a name="line409"></a> * matching dataPath and matching id
<a name="line410"></a> *
<a name="line411"></a> * @param {Function} fn Callback function, signature function(dataPath, id).
<a name="line412"></a> * @param {string=} opt_dataPath Fully qualified data path.
<a name="line413"></a> * @param {string=} opt_id A value passed back to the listener when the dataPath
<a name="line414"></a> *   is matched.
<a name="line415"></a> */
<a name="line416"></a>goog.ds.DataManager.prototype.removeListeners = function(fn, opt_dataPath,
<a name="line417"></a>    opt_id) {
<a name="line418"></a>
<a name="line419"></a>  // Normalize data path root
<a name="line420"></a>  if (opt_dataPath &amp;&amp; goog.string.endsWith(opt_dataPath, &#39;/...&#39;)) {
<a name="line421"></a>    opt_dataPath = opt_dataPath.substring(0, opt_dataPath.length - 4);
<a name="line422"></a>  } else if (opt_dataPath &amp;&amp; goog.string.endsWith(opt_dataPath, &#39;/*&#39;)) {
<a name="line423"></a>    opt_dataPath = opt_dataPath.substring(0, opt_dataPath.length - 2);
<a name="line424"></a>  }
<a name="line425"></a>
<a name="line426"></a>  this.removeListenersByFunction_(
<a name="line427"></a>      this.listenersByFunction_, false, fn, opt_dataPath, opt_id);
<a name="line428"></a>};
<a name="line429"></a>
<a name="line430"></a>
<a name="line431"></a>/**
<a name="line432"></a> * Removes listeners with a given callback function, and optional
<a name="line433"></a> * matching dataPath and matching id from the given listenersByFunction
<a name="line434"></a> * data structure.
<a name="line435"></a> *
<a name="line436"></a> * @param {Object} listenersByFunction The listeners by function.
<a name="line437"></a> * @param {boolean} indexed Indicates whether the listenersByFunction are
<a name="line438"></a> *     indexed or not.
<a name="line439"></a> * @param {Function} fn Callback function, signature function(dataPath, id).
<a name="line440"></a> * @param {string=} opt_dataPath Fully qualified data path.
<a name="line441"></a> * @param {string=} opt_id A value passed back to the listener when the dataPath
<a name="line442"></a> *   is matched.
<a name="line443"></a> * @private
<a name="line444"></a> */
<a name="line445"></a>goog.ds.DataManager.prototype.removeListenersByFunction_ = function(
<a name="line446"></a>    listenersByFunction, indexed, fn, opt_dataPath, opt_id) {
<a name="line447"></a>  var fnUid = goog.getUid(fn);
<a name="line448"></a>  var functionMatches = listenersByFunction[fnUid];
<a name="line449"></a>  if (functionMatches != null) {
<a name="line450"></a>    for (var key in functionMatches) {
<a name="line451"></a>      var functionMatch = functionMatches[key];
<a name="line452"></a>      var listener = functionMatch.listener;
<a name="line453"></a>      if ((!opt_dataPath || opt_dataPath == listener.dataPath) &amp;&amp;
<a name="line454"></a>          (!opt_id || opt_id == listener.id)) {
<a name="line455"></a>        if (indexed) {
<a name="line456"></a>          this.removeListeners(
<a name="line457"></a>              listener.fn, listener.dataPath, listener.id);
<a name="line458"></a>        }
<a name="line459"></a>        if (functionMatch.items) {
<a name="line460"></a>          for (var i = 0; i &lt; functionMatch.items.length; i++) {
<a name="line461"></a>            var item = functionMatch.items[i];
<a name="line462"></a>            delete item.obj[item.key];
<a name="line463"></a>          }
<a name="line464"></a>        }
<a name="line465"></a>        delete functionMatches[key];
<a name="line466"></a>      }
<a name="line467"></a>    }
<a name="line468"></a>  }
<a name="line469"></a>};
<a name="line470"></a>
<a name="line471"></a>
<a name="line472"></a>/**
<a name="line473"></a> * Get the total number of listeners (per expression listened to, so may be
<a name="line474"></a> * more than number of times addListener() has been called
<a name="line475"></a> * @return {number} Number of listeners.
<a name="line476"></a> */
<a name="line477"></a>goog.ds.DataManager.prototype.getListenerCount = function() {
<a name="line478"></a>  var count = 0;
<a name="line479"></a>  goog.structs.forEach(this.listenerMap_, function(matchingListeners) {
<a name="line480"></a>    count += goog.structs.getCount(matchingListeners);
<a name="line481"></a>  });
<a name="line482"></a>  return count;
<a name="line483"></a>};
<a name="line484"></a>
<a name="line485"></a>
<a name="line486"></a>/**
<a name="line487"></a> * Disables the sending of all data events during the execution of the given
<a name="line488"></a> * callback. This provides a way to avoid useless notifications of small changes
<a name="line489"></a> * when you will eventually send a data event manually that encompasses them
<a name="line490"></a> * all.
<a name="line491"></a> *
<a name="line492"></a> * Note that this function can not be called reentrantly.
<a name="line493"></a> *
<a name="line494"></a> * @param {Function} callback Zero-arg function to execute.
<a name="line495"></a> */
<a name="line496"></a>goog.ds.DataManager.prototype.runWithoutFiringDataChanges = function(callback) {
<a name="line497"></a>  if (this.disableFiring_) {
<a name="line498"></a>    throw Error(&#39;Can not nest calls to runWithoutFiringDataChanges&#39;);
<a name="line499"></a>  }
<a name="line500"></a>
<a name="line501"></a>  this.disableFiring_ = true;
<a name="line502"></a>  try {
<a name="line503"></a>    callback();
<a name="line504"></a>  } finally {
<a name="line505"></a>    this.disableFiring_ = false;
<a name="line506"></a>  }
<a name="line507"></a>};
<a name="line508"></a>
<a name="line509"></a>
<a name="line510"></a>/**
<a name="line511"></a> * Fire a data change event to all listeners
<a name="line512"></a> *
<a name="line513"></a> * If the path matches the path of a listener, the listener will fire
<a name="line514"></a> *
<a name="line515"></a> * If your path is the parent of a listener, the listener will fire. I.e.
<a name="line516"></a> * if $Contacts/bob@bob.com changes, then we will fire listener for
<a name="line517"></a> * $Contacts/bob@bob.com/Name as well, as the assumption is that when
<a name="line518"></a> * a parent changes, all children are invalidated.
<a name="line519"></a> *
<a name="line520"></a> * If your path is the child of a listener, the listener may fire, depending
<a name="line521"></a> * on the ancestor depth.
<a name="line522"></a> *
<a name="line523"></a> * A listener for $Contacts might only be interested if the contact name changes
<a name="line524"></a> * (i.e. $Contacts doesn&#39;t fire on $Contacts/bob@bob.com/Name),
<a name="line525"></a> * while a listener for a specific contact might
<a name="line526"></a> * (i.e. $Contacts/bob@bob.com would fire on $Contacts/bob@bob.com/Name).
<a name="line527"></a> * Adding &quot;/...&quot; to a lisetener path listens to all children, and adding &quot;/*&quot; to
<a name="line528"></a> * a listener path listens only to direct children
<a name="line529"></a> *
<a name="line530"></a> * @param {string} dataPath Fully qualified data path.
<a name="line531"></a> */
<a name="line532"></a>goog.ds.DataManager.prototype.fireDataChange = function(dataPath) {
<a name="line533"></a>  if (this.disableFiring_) {
<a name="line534"></a>    return;
<a name="line535"></a>  }
<a name="line536"></a>
<a name="line537"></a>  var expr = goog.ds.Expr.create(dataPath);
<a name="line538"></a>  var ancestorDepth = 0;
<a name="line539"></a>
<a name="line540"></a>  // Look for listeners for expression and all its parents.
<a name="line541"></a>  // Parents of listener expressions are all added to the listenerMap as well,
<a name="line542"></a>  // so this will evaluate inner loop every time the dataPath is a child or
<a name="line543"></a>  // an ancestor of the original listener path
<a name="line544"></a>  while (expr) {
<a name="line545"></a>    var matchingListeners = this.listenerMap_[expr.getSource()];
<a name="line546"></a>    if (matchingListeners) {
<a name="line547"></a>      for (var id in matchingListeners) {
<a name="line548"></a>        var match = matchingListeners[id];
<a name="line549"></a>        var listener = match.listener;
<a name="line550"></a>        if (ancestorDepth &lt;= match.maxAncestors) {
<a name="line551"></a>          listener.fn(dataPath, listener.id);
<a name="line552"></a>        }
<a name="line553"></a>      }
<a name="line554"></a>    }
<a name="line555"></a>    ancestorDepth++;
<a name="line556"></a>    expr = expr.getParent();
<a name="line557"></a>  }
<a name="line558"></a>  this.eventCount_++;
<a name="line559"></a>};
</pre>


</body>
</html>
